/*
 * FriendsPage.fx
 *
 * Created on Feb 28, 2010, 8:08:05 PM
 */
package com.gpsbuddy.navigation;

import javafx.scene.Scene;
import com.gpsbuddy.main.Enviroment;
import javafx.geometry.HPos;
import javafx.scene.layout.VBox;
import javafx.scene.control.CheckBox;
import javafx.scene.control.Button;
import com.gpsbuddy.utils.DatabaseUtils;
import com.gpsbuddy.components.CustomTextBox;
import com.gpsbuddy.services.User;
import javafx.animation.Interpolator;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import com.gpsbuddy.data.Property;
import com.gpsbuddy.main.Main;
import com.gpsbuddy.utils.ServiceUtils;
import com.gpsbuddy.utils.Utils;
import javafx.scene.Group;
import com.gpsbuddy.components.Accordion;
import com.gpsbuddy.components.AccordionItem;
import javafx.scene.control.ListView;

/**
 * @author jonycus
 */
var fList: ListView;
var rList: ListView;
var fsList:ListView;
var items: Object[];
var reqs: Object[];
var friends: Object[];
public var foundUsers: User[] on replace {
            println("found user: {sizeof foundUsers}");

            for (i: User in foundUsers) {
                items[indexof i] = "{i.name} {i.surname}";
            }

            visible = true;
            fList.items = items;
        };

public var foundRequests: User[] on replace {
        println("requests: {sizeof foundRequests}");
            for (i: User in foundRequests) {
                reqs[indexof i] = "{i.name} {i.surname}";
            }

            visible = true;
            rList.items = reqs;
        };

public var foundFriends: User[] on replace {
        println("requests: {sizeof foundFriends}");
            for (i: User in foundFriends) {
                friends[indexof i] = "{i.name} {i.surname}";
            }

            visible = true;
            fsList.items = friends;
        };

var visible = false;

public class FriendsPage extends Scene {

    public-init var env: Enviroment;
    var op: Number = 0;
    var lX: Number = -env.screenWidth;
    var beVisible = CheckBox {
                text: "Allow my frends to see my possition?"
            }
    var okButton = Button {
                text: "Ok";
                action: storeSettings;
            }
    var friendName = CustomTextBox {
                env: env;
                label: "Name:"
            }
    var friendSurname = CustomTextBox {
                env: env;
                label: "Name:"
            }
    var findFriendButoon = Button {
                text: "Find"
                action: findFriends
            }
    var visibilityContentBox = VBox {
                layoutY: 30;
                nodeHPos: HPos.CENTER;
                spacing: 10;
                content: [beVisible, okButton]
            }
    var mainNavigationBar = MainStatusBar {
                env: env;
                width: bind env.screenWidth;
                height: bind env.screenHeight;
                barHeight: 30
            }
    //var scroller =
    //var foundFriendsList =
    var findFriendsContentBox = VBox {
                layoutX: 10;
                layoutY: 15;
                nodeHPos: HPos.CENTER;
                spacing: 5;
                content: [friendName, friendSurname, findFriendButoon]
            }
    var makeFriendRequsetButton = Button {
                text: "Ask to be your friend!"
                action: makeFriendRequest
                layoutY: 170
            }
    var approveButoon = Button {
                layoutY: 140;
                text: "Approve!"
                action: approveFriendRequest;
            }
    var denyButoon = Button {
                text: "Reject!"
                layoutY: 140;
                layoutX: 90
                action: findFriends
            }
    var accordion = Accordion {
                itemHeight: 25;
                itemWidth: env.screenWidth;
                height: getHeight();
                itemSpacing: 1;
                items: [
                    AccordionItem { title: "Visibility" opened: true content: [visibilityContentBox] },
                    AccordionItem { title: "Friends" content: [fsList = ListView {
                                items: friends
                                width: env.screenWidth - 60
                                layoutY: 60
                                height: getHeight() - 220;
                                }
                            ] },
                    AccordionItem { title: "Find friends" content: [findFriendsContentBox, fList = ListView {
                                items: items
                                width: env.screenWidth - 60
                                layoutY: 100
                                height: getHeight() - 220;
                            }, makeFriendRequsetButton] }
                    AccordionItem { title: "Requests" content: [rList = ListView {
                                items: reqs
                                width: env.screenWidth - 60
                                layoutY: 60
                                height: getHeight() - 220;
                            }, approveButoon, denyButoon] },
                ]
            }
    var group = Group {
                layoutX: bind lX;
                opacity: bind op;
                content: [mainNavigationBar, accordion];
            }

    postinit {
        if (DatabaseUtils.initialized) {
            fillStoredFields();
        }
        content = [group];
    }

    public function transitIn() {
        Timeline {
            keyFrames: [
                KeyFrame {
                    time: 0s
                    values: [lX => -env.screenWidth tween Interpolator.LINEAR, op => 0.0 tween Interpolator.LINEAR];
                }
                KeyFrame {
                    time: 0.5s
                    values: [lX => 0 tween Interpolator.LINEAR, op => 1.0 tween Interpolator.LINEAR];
                }
            ]
        }.play();
    }

    public function storeSettings() {
        if (DatabaseUtils.hasProp("isVisible")) {
            DatabaseUtils.updateProp("isVisible", beVisible.selected.toString());
        } else {
            DatabaseUtils.addProp("isVisible", beVisible.selected.toString());
        }
        Main.switchToFrontPage();
        DatabaseUtils.storeData();
    }

    function fillStoredFields() {
        for (prop: Property in DatabaseUtils.props) {
            if (prop.name.equals("isVisible")) {
                beVisible.selected = prop.value.equals("true");
            }
        }
    }

    function getHeight(): Number {
        if (Utils.isMobile) {
            return env.screenHeight - mainNavigationBar.barHeight;
            } else {
            return env.screenHeight - 37 - mainNavigationBar.barHeight;
            }
    }

    function findFriends(): Void {
        var name = friendName.text;
        var surname = friendSurname.text;
        ServiceUtils.findFriends(name, surname);
    }

    function makeFriendRequest() {
        var rUser = Enviroment.loggedUser;
        var tUser = foundUsers[fList.selectedIndex];
        ServiceUtils.makeFriendRequest(rUser, tUser);
    }

    function approveFriendRequest(){
        var rUser = Enviroment.loggedUser;
        var tUser = foundRequests[rList.selectedIndex];
        ServiceUtils.acceptFriendRequest(rUser, tUser);
    }


}
